//
//  YHHomeController.swift
//  WeiBoYH
//
//  Created by yanhui on 2017/1/4.
//  Copyright © 2017年 yanhui. All rights reserved.
//

import UIKit
import SVProgressHUD
import YYModel

//定义可重用标识符
private let homeCellId = "YHStatusCellId"

class YHHomeController: YHBaseHomeController {
    
    lazy var homeViewModel = YHHomeViewModel()

    
    override func viewDidLoad() {
        super.viewDidLoad()
        //判断用户是否登录
        if !userLogin {
        visitorLoginView.updataInfo(imageName: "visitordiscover_feed_image_smallicon", title: "祖国人民的花朵你们辛苦了", isHome: true)
            return
        }

        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "", imageName: "navigationbar_pop", target: self, action: #selector(nextPage))
        //设置tableView
        setupTableView()
        
        //加载数据
        self.homeViewModel.loadData { (success) in
            if !success {
                SVProgressHUD.showError(withStatus: "世界上最遥远的距离就是没有网络")
                return
            }
            //数组已经有元素但是刷新没有显示结果
            self.tableView.reloadData()
        }

    }

    //设置tableView
    private func setupTableView() {
        let cellNib = UINib(nibName: "YHStatusCell", bundle: nil)
        self.tableView.register(cellNib, forCellReuseIdentifier: "YHStatusCellId")
        //设置行高
        self.tableView.rowHeight = 500
    }
    @objc private func nextPage() {
        //进入子页面
        let demo = YHDemoController()
        
        self.navigationController?.pushViewController(demo, animated: true)
    }
   
}

extension YHHomeController {
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.homeViewModel.viewmodelArray.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: homeCellId, for: indexPath) as! YHStatusCell
        let viewmodel = self.homeViewModel.viewmodelArray[indexPath.row]
        cell.viewmodel = viewmodel
        
        return cell
    }
}

